<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<c:set var="url" value="${pageContext.request.contextPath}/" />
<link rel="stylesheet" href="${url }plugins/select2/select2.min.css">
<script src="${url }plugins/select2/select2.full.min.js"></script>
<script type="text/javascript">
$(function() {
	init();
	//initTables();
});

function init(){
	var addIp =$("#addIp").val();
	$.ajax({
		type:"POST",
		url: path+'collect/getBeatInfo',
		data:{ip:addIp,beatName:'ptdbbeat'},
		success: function(result){
			$("#nodeTbody").html("");
			for(var i = 0;i<result.result.ptdbbeat.parameter.list.length;i++){
				var appendStr =" <tr><td>"+result.result.ptdbbeat.parameter.list[i].type+"</td>";
				appendStr+="<td>"+result.result.ptdbbeat.parameter.list[i].host+"</td>";
				appendStr+="<td>"+result.result.ptdbbeat.parameter.list[i].port+"</td>";
				appendStr+="<td>"+result.result.ptdbbeat.parameter.list[i].name+"</td>";
				appendStr+="<td>"+result.result.ptdbbeat.parameter.list[i].user+"</td>";
				appendStr+="<td>"+result.result.ptdbbeat.parameter.list[i].table+"</td>";
				appendStr+="<td>"+result.result.ptdbbeat.parameter.list[i].field+"</td>";
				appendStr+="<td>"+result.result.ptdbbeat.parameter.list[i].sql+"</td>";
				appendStr+="<td>"+result.result.ptdbbeat.parameter.list[i].desc+"</td>";
				appendStr+="<td><a href='#' title='删除' onclick='deleteBeatInfo(\""+result.result.ptdbbeat.parameter.list[i].type+"\",\""+result.result.ptdbbeat.parameter.list[i].host+"\",\""+result.result.ptdbbeat.parameter.list[i].port+"\",\""+result.result.ptdbbeat.parameter.list[i].name+"\",\""+result.result.ptdbbeat.parameter.list[i].user+"\",\""+result.result.ptdbbeat.parameter.list[i].table+"\",\""+result.result.ptdbbeat.parameter.list[i].field+"\",\""+result.result.ptdbbeat.parameter.list[i].sql+"\",\""+result.result.ptdbbeat.parameter.list[i].desc+"\",\""+result.result.ptdbbeat.parameter.list[i].passwd+"\")'><i class='fa fa-play text-red'></i>删除</a></td></tr>";
				$("#nodeTbody").append(appendStr);
			}
		}
	}); 
}

function deleteBeatInfo(type,host,port,name,user,table,field,sql,desc,password){
	$("#rmtype").val(type);
	$("#rmhost").val(host);
	$("#rmport").val(port);
	$("#rmname").val(name);
	$("#rmuser").val(user);
	$("#rmtable").val(table);
	$("#rmfield").val(field);
	$("#rmsql").val(sql);
	$("#rmdesc").val(desc);
	$("#rmpassword").val(password);
	$.ajax({
		type:"POST",
		url: path+'collect/deletePtbBeatInfo',
		data:$('#delBeatInfoForm').serialize(),
		success: function(result){
			if(result){
				alert('删除成功');
				init();
			}
		}
	});
}

function initTables(){
	if($("#dbType").val()!=""&&$("#dbIp").val()!=""&&$("#dbPort").val()!=""&&$("#dbName").val()!=""&&$("#user").val()!=""&&$("#password").val()!=""){
		$.ajax({
			cache:false,
			async:false,
			type:"POST",
			url: path+'collect/getAllTableName',
			data:$('#dbForm').serialize(),
			success: function(result){
				if(result){
					$("#addTableName").html("");
					for(var i=0;i<result.tables.length;i++){
						var appendStr="<option value='"+result.tables[i].name+"'>"+result.tables[i].name+"</option>";
						$("#addTableName").append(appendStr);
					}
				}
			}
		});
	}
}

function findTableCells(){
	var tableName=$("#addTableName").val();
	$.ajax({
		cache:false,
		async:false,
		type:"POST",
		url: path+'collect/getTableCellByName',
		data:{tableName:tableName},
		success: function(result){
			if(result){
				$("#tableCell").html("");
				for(var i=0;i<result.fields.length;i++){
					var appendStr="<option value='"+result.fields[i].name+"'>"+result.fields[i].name+"</option>";
					$("#tableCell").append(appendStr);
				}
			}
		}
	});
}

function addBeat(){
	if($("#dbType").val()==""){
		alert("数据库类型不能为空");
		return;
	}
	if($("#dbIp").val()==""){
		alert("数据库IP不能为空");
		return;
	}
	if($("#dbPort").val()==""){
		alert("数据库端口不能为空");
		return;
	}
	if($("#dbName").val()==""){
		alert("数据库名称不能为空");
		return;
	}
	if($("#user").val()==""){
		alert("用户名不能为空");
		return;
	}
	if($("#password").val()==""){
		alert("密码不能为空");
		return;
	}
	if($("#addTableName").val()==""){
		alert("数据库表名不能为空");
		return;
	}
	if($("#tableCell").val()==""){
		alert("增加字段不能为空");
		return;
	}
	if($("#sql").val()==""){
		alert("SQL不能为空");
		return;
	}
	if($("#desc").val()==""){
		alert("描述不能为空");
		return;
	}
	
	$.ajax({
		cache:false,
		async:false,
		type:"POST",
		url: path+"collect/addPtdBeat",
		data:$('#dbForm').serialize(),
		success: function(result){
			if(result){
				alert("添加成功");
				init();
				initTables();
			}
		}
	});
}
</script>
<div class="box box-primary">
	<div class="box-header with-border">
		<h3 class="box-title">TcpBeat / </h3>
		<!-- /.box-tools -->
	</div>
	
  <!-- /.box-header -->
  <div class="box-body no-padding">
    <div class="table-responsive mailbox-messages">
      <table class="table table-bordered">
      	<thead>
      		<tr style="background-color: #f9f9f9;">
      		<th>TYPE</th>
      		<th>HOST</th>
      		<th>PORT</th>
      		<th>DBNAME</th>
      		<th>USER</th>
      		<th>TABLE</th>
      		<th>FIELD</th>
      		<th>SQL</th>
      		<th>描述</th>
      		<th>删除</th>
      		</tr>
      	</thead>
        <tbody id="nodeTbody">
        
        </tbody>
      </table>
      <!-- /.table -->
    </div>
    <!-- /.mail-box-messages -->
  </div>
  <!-- /.box-body -->
  
</div>

<div class="box box-info">
<!-- /.box-header -->
<form id="dbForm">
  <div class="box-body form-horizontal">
  	<div class="form-group">
       <label for="inputEmail3" class="col-sm-2 control-label">数据库类型</label>
       <div class="col-sm-3">
         <input type="text" class="form-control" id="dbType" placeholder="数据库类型" name="type"  onblur="initTables()">
       </div>
     </div>
     <div class="form-group">
       <label for="inputEmail3" class="col-sm-2 control-label">数据库IP</label>
       <div class="col-sm-3">
         <input type="text" class="form-control" id="dbIp" placeholder="数据库IP" name="ip"  onblur="initTables()">
       </div>
     </div>
     <div class="form-group">
       <label for="inputEmail3" class="col-sm-2 control-label">数据库端口</label>
       <div class="col-sm-3">
         <input type="text" class="form-control" id="dbPort" placeholder="数据库端口" name="port"  onblur="initTables()">
       </div>
     </div>
     <div class="form-group">
       <label for="inputEmail3" class="col-sm-2 control-label">数据库名称</label>
       <div class="col-sm-3">
         <input type="text" class="form-control" id="dbName" placeholder="数据库名称" name="dbName"  onblur="initTables()">
       </div>
     </div>
     <div class="form-group">
       <label for="inputEmail3" class="col-sm-2 control-label">用户名</label>
       <div class="col-sm-3">
         <input type="text" class="form-control" id="user" placeholder="用户名" name="user"  onblur="initTables()">
       </div>
     </div>
     <div class="form-group">
       <label for="inputEmail3" class="col-sm-2 control-label">密码</label>
       <div class="col-sm-3">
         <input type="password" class="form-control" id="password" placeholder="密码" name="password" onblur="initTables()">
       </div>
     </div>
  	<div class="form-group">
       <label for="inputEmail3" class="col-sm-2 control-label">数据库表</label>
       <div class="col-sm-3">
         <select id="addTableName" name="addTableName" class="form-control select2" onchange="findTableCells()">
			
		</select>
       </div>
     </div>
  	<div class="form-group">
       <label for="inputEmail3" class="col-sm-2 control-label">增量字段</label>
       <div class="col-sm-3">
         <select id="tableCell" name="tableCell" class="form-control select2">
			
		</select>
       </div>
     </div>
     <div class="form-group">
       <label for="inputEmail3" class="col-sm-2 control-label">SQL</label>
       <div class="col-sm-5">
         <input type="text"  class="form-control" name="sql"  id="sql"  placeholder="SQL">
       </div>
     </div>
     <div class="form-group">
       <label for="inputEmail3" class="col-sm-2 control-label">描述</label>
       <div class="col-sm-5">
         <input type="text" class="form-control" id="desc" name="desc" placeholder="描述">
       </div>
     </div>
      <div class="box-footer">
        <a type="button" class="btn btn-info"  onclick="addBeat()">添加</a>
        <a href="javascript:linkByDom('local-list','${url }collect/getBeatList');" class="btn btn-default">取消</a>
     	<input type="hidden" name="target" value="${ip }"  />
     </div>
  </div>
  </form>
</div>
<input type="hidden" value="${ip }" id="addIp" />			            
<form id="delBeatInfoForm">
	<input type="hidden" id="rmtype" name="type" />
	<input type="hidden" id="rmhost" name="host" />
	<input type="hidden" id="rmport" name="port" />
	<input type="hidden" id="rmname" name="name" />
	<input type="hidden" id="rmuser" name="user" />
	<input type="hidden" id="rmtable" name="table" />
	<input type="hidden" id="rmfield" name="field" />
	<input type="hidden" id="rmsql" name="sql" />
	<input type="hidden" id="rmdesc" name="desc" />
	<input type="hidden" id="rmpassword" name="password" />
</form>
